#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2024/5/23 11:47
# @Author : 闫峰
# @File    : batch_by_mongodb.py
from utils.mysqlUtils.mongodb_utils import MongoDBLink


class MongoDBByBatch:


    def __init__(self,MongoDBLink):
        self.MongoDBLink = MongoDBLink
    def get_data(self,filters,projection,limit):
        res_id_index = self.MongoDBLink.select_skip_projection_limit(filters,projection, "_id", 1)
        if not res_id_index:
            return
        first_data = res_id_index
        _id_index = res_id_index[0]['_id']
        while True:
            filters['$and'].append({"_id": {"$gt": _id_index}})
            actual_sku_list = self.MongoDBLink.select_skip_projection_limit(filters,projection, "_id", limit)
            if first_data:
                actual_sku_list.insert(0, first_data[0])
                first_data.clear()
            if not len(actual_sku_list):
                break
            _id_index = actual_sku_list[-1].get('_id')
            yield actual_sku_list


